package com.efounder.news.http;

import com.efounder.mobilenews.R;
import com.efounder.news.bean.CommentListBean;
import com.efounder.news.bean.CommentReplyBean;
import com.efounder.news.bean.NewsListBean;
import com.efounder.news.utils.LogUtils;
import com.efounder.utils.ResStringUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import static android.content.ContentValues.TAG;

/**
 * 解析Json的工具类
 * sunlp on 2017/11/2
 */

public class DataParse {

    /**
     * 解析新闻列表
     * @param json
     * @return
     * {
    "id":"967ab8de49534276710b07c4d11afbcdc5f870bac5aa0418a046487aceec848a",
    "title":"护理学院组织上党课知党史、学党章、强党性学习",
    "date":"2017-12-03",
    "read_count":0,
    "media_name":"山大视点",
    "comment_count":"1",
    "image_list":[

    ]
     */
    public static ArrayList<NewsListBean> getNewsList(String json){
        if (json != null) {
            ArrayList<NewsListBean> newsListBeans = new ArrayList<>();
            try {
                JSONArray array = new JSONArray(json);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject newJson =  array.getJSONObject(i);
                    String newId = newJson.getString("id");
                    String newTitle = newJson.getString("title");
                    String newDate = newJson.getString("date");
                    String readCount = newJson.getString("read_count");
                    String mediaName = newJson.getString("media_name");
                    String commentCount = newJson.getString("comment_count");

                    JSONArray imageArray = newJson.getJSONArray("image_list");

                    ArrayList<NewsListBean.ImgBean> imgList = new ArrayList<>();
                    for(int j = 0; j < imageArray.length(); j++){
                        JSONObject imgJson = imageArray.getJSONObject(j);
                        String imgUrl = imgJson.getString("url");
                        NewsListBean.ImgBean imgBean = new NewsListBean.ImgBean();
                        imgBean.setImgsrc(imgUrl);

                        imgList.add(imgBean);
                    }
                    NewsListBean newsListBean = new NewsListBean();

                    newsListBean.setNewId(newId);
                    newsListBean.setTitle(newTitle);
                    newsListBean.setNewsTime(newDate);
                    newsListBean.setInteractions(commentCount);
                    newsListBean.setFromWhere(mediaName);
                    newsListBean.setImgBeanList(imgList);

                    newsListBeans.add(newsListBean);
                }
                return newsListBeans;
            } catch (JSONException e) {
                e.printStackTrace();
                LogUtils.d(TAG, ResStringUtil.getString(R.string.log_tip_json_error));
            }
        } else {
            LogUtils.d(TAG, ResStringUtil.getString(R.string.log_tip_json_nodata));
        }
        return null;
    }

    /**
     * 评论后返回的数据
     * @param json
     * @return
     * {"status":"suceess"}
     */
    public static String commentReturn(String json){
        if(json != null){
            try {
                JSONObject jsonObject = new JSONObject(json);
                String commentReturn = jsonObject.getString("message");
                if (commentReturn.equals("success")){
                    return ResStringUtil.getString(R.string.detail_comment_success);
                }else{
                    return ResStringUtil.getString(R.string.detail_comment_failed);
                }
            }catch (JSONException e){
                e.printStackTrace();
                LogUtils.d(TAG, ResStringUtil.getString(R.string.log_tip_json_error));
            }
        }else {
            LogUtils.d(TAG, ResStringUtil.getString(R.string.log_tip_json_nodata));
        }
        return null;
    }

    /**
     * @param json
     * @return
     * "comment":{
    "date":"2017-11-29 16:52:18",
    "reply_list":[
    {
    "date":"2017-11-29 17:09:11",
    "user_id":"78363",
    "digg_count":0,
    "user_name":"测试号",
    "id":"8992f6a8-3a08-46b0-9130-f27b7f5da90d",
    "text":"回复2"
    },
    {
    "date":"2017-11-29 17:08:59",
    "user_id":"78363",
    "digg_count":0,
    "user_name":"测试号",
    "id":"0f0c4d59-926e-4282-972d-6768b1dea1c4",
    "text":"回复1"
    }
    ],
    "user_id":"22",
    "digg_count":0,
    "user_name":"",
    "parent_id":"",
    "id":"a7ce73aa-194c-470a-bd14-007923b7d30f",
    "text":"4",
    "reply_count":0
    "user_digg":"1"
    }
     */
    public static ArrayList<CommentListBean> getCommentList(String json){
        if (json != null) {
            JSONObject jsonObject = null;
            ArrayList<CommentListBean> commentListBeans = new ArrayList<>();
            try {
                jsonObject = new JSONObject(json);
                JSONArray array = jsonObject.getJSONArray("data");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject newJson =  array.getJSONObject(i);
                    JSONObject commentJson = newJson.getJSONObject("comment");

                    String commentDate = commentJson.getString("date");
                    String commentId = commentJson.getString("id");
                    String comment = commentJson.getString("text");
                    String userId = commentJson.getString("user_id");
//                    String parentId = commentJson.getString("parent_id");
                    String userName = commentJson.getString("user_name");
                    String diggCount = commentJson.getString("digg_count");
                    String replyCount = commentJson.getString("reply_count");
                    String userDigg = commentJson.getString("user_digg");
                    String integral = commentJson.optString("integral");

                    JSONArray replyArray = commentJson.getJSONArray("reply_list");
                    ArrayList<CommentListBean.replyComment> replyList = new ArrayList();
                    for (int j = 0;j < replyArray.length() ; j++){
                        JSONObject replyJson = replyArray.getJSONObject(j);

                        String reDate = replyJson.getString("date");
                        String reUserId = replyJson.getString("user_id");
                        String reDigCount = replyJson.getString("digg_count");
                        String reUserName = replyJson.getString("user_name");
                        String reId = replyJson.getString("id");
                        String reText = replyJson.getString("text");

                        CommentListBean.replyComment replyComment = new CommentListBean.replyComment();

                        replyComment.setDate(reDate);
                        replyComment.setDiggCount(reDigCount);
                        replyComment.setUserId(reUserId);
                        replyComment.setUserName(reUserName);
                        replyComment.setId(reId);
                        replyComment.setText(reText);

                        replyList.add(replyComment);
                    }


                    CommentListBean commentListBean = new CommentListBean();

//                    commentListBean.setNewsId(newId);
                    commentListBean.setReplyList(replyList);
                    commentListBean.setCommentDate(commentDate);
                    commentListBean.setCommentId(commentId);
                    commentListBean.setComment(comment);
                    commentListBean.setUserId(userId);
                    commentListBean.setIntegral(integral);
//                    commentListBean.setParentId(parentId);
                    commentListBean.setUserName(userName);
                    commentListBean.setDiggCount(diggCount);
                    commentListBean.setReplyCount(replyCount);
                    commentListBean.setUserDigg(userDigg);

                    commentListBeans.add(commentListBean);
                }
                return commentListBeans;
            } catch (JSONException e) {
                e.printStackTrace();
                LogUtils.d(TAG, ResStringUtil.getString(R.string.log_tip_json_error));
            }
        } else {
            LogUtils.d(TAG, ResStringUtil.getString(R.string.log_tip_json_nodata));
        }
        return null;
    }

    /**
     * {
     "data":{
     "data":[
     {
     "date":"2017-12-04 16:22:49",
     "reply_to_comment":[
     {
     "date":"2017-12-04 16:19:59",
     "user_id":"",
     "user_name":"",
     "id":"0b2f43d5-63a9-4d8e-b12d-0000e67e7b2a",
     "text":"1"
     }
     ],
     "reply_id":"0b2f43d5-63a9-4d8e-b12d-0000e67e7b2a",
     "user_id":"",
     "digg_count":0,
     "user_name":"",
     "id":"83fe0927-2016-4940-a292-ae52828e2797",
     "text":"11",
     "user_digg":"0"
     },
     * @param json
     * @return
     */
    public static ArrayList<CommentReplyBean> getCommentReply(String json){
        if (json != null) {
            JSONObject jsonObject = null;
            ArrayList<CommentReplyBean> commentReplyBeans = new ArrayList<>();
            try {
                jsonObject = new JSONObject(json);
                JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                JSONArray array = jsonObject1.getJSONArray("data");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject commentJson =  array.getJSONObject(i);

                    String commentDate = commentJson.getString("date");
                    String commentId = commentJson.getString("id");
                    String comment = commentJson.getString("text");
                    String userId = commentJson.getString("user_id");
                    String userName = commentJson.getString("user_name");
                    String diggCount = commentJson.getString("digg_count");
                    String userDigg = commentJson.getString("user_digg");

                    JSONArray replyArray = commentJson.getJSONArray("reply_to_comment");
                    ArrayList<CommentReplyBean.ReplyReplyBean> replyList = new ArrayList();
                    if(replyArray!=null){
                        /**
                         * "date":"2017-12-04 16:19:59",
                         "user_id":"2982",
                         "user_name":"孙立鹏",
                         "id":"0b2f43d5-63a9-4d8e-b12d-0000e67e7b2a",
                         "text":"1"
                         */
                        for (int j = 0;j < replyArray.length() ; j++){
                            JSONObject replyJson = replyArray.getJSONObject(j);
                            String rDate = replyJson.getString("date");
                            String rUserId = replyJson.getString("user_id");
                            String rUserName = replyJson.getString("user_name");
                            String rId = replyJson.getString("id");
                            String rText = replyJson.getString("text");

                            CommentReplyBean.ReplyReplyBean replyBean = new CommentReplyBean.ReplyReplyBean();
                            replyBean.setDate(rDate);
                            replyBean.setId(rId);
                            replyBean.setText(rText);
                            replyBean.setUserId(rUserId);
                            replyBean.setUserName(rUserName);

                            replyList.add(replyBean);

                        }
                    }
                    CommentReplyBean commentReplyBean = new CommentReplyBean();
                    commentReplyBean.setReplyList(replyList);
                    commentReplyBean.setDate(commentDate);
                    commentReplyBean.setId(commentId);
                    commentReplyBean.setText(comment);
                    commentReplyBean.setUserId(userId);
                    commentReplyBean.setUserName(userName);
                    commentReplyBean.setDiggCount(diggCount);
                    commentReplyBean.setUserDigg(userDigg);

                    commentReplyBeans.add(commentReplyBean);
                }
                return commentReplyBeans;
            } catch (JSONException e) {
                e.printStackTrace();
                LogUtils.d(TAG, ResStringUtil.getString(R.string.log_tip_json_error));
            }
        } else {
            LogUtils.d(TAG, ResStringUtil.getString(R.string.log_tip_json_nodata));
        }
        return null;
    }

}
